package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entity.Movie;
import util.Database;

public class UserDao {	
	
	public String login(String username,String password){
		String sql = "select* from user where username='"+username+"' and password='"+password+"'"; //" AND password= '"+password+"'";
		ResultSet rs =Database.query(sql);
		String b = null;
		try{
			if(rs.next())
			b = rs.getString("id");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			
		}
		return b;	
	}
	
	public String reg(String username,String password){
		UserDao dao = new UserDao();
		String r = null;
		String b = dao.login(username, password);
		try{
			if(b==null){
				try{
					String sql = "insert into user(username,password) values('"+username+"','"+password+"')";
					Database.execute(sql);
					r = dao.login(username, password);
				}
				catch (Exception e){
					e.printStackTrace();
				}
				
			}
			else{
				
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return r;
	}
	
	public String findById(String id){
		String sql ="select username from user where id = '"+id+"'";
		ResultSet rs =Database.query(sql);
		String username = null;
		try{
			if(rs.next())
			username = rs.getString("username");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return username;
	}
	
	public String findPassword(String id){
		String sql ="select password from user where id = '"+id+"'";
		ResultSet rs =Database.query(sql);
		String password = null;
		try{
			if(rs.next())
				password = rs.getString("password");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return password;
	}
	
	public ArrayList<Movie> findPurchase(String uid) {
		ArrayList<Movie> list = new ArrayList<Movie>();
		String sql ="select * from movie where mid in (select mid from purchase where id = '"+uid+"')";
		ResultSet rs = Database.query(sql);
		/*String mid = null;
		try {
			while(rs.next()){
				mid = rs.getString("mid");
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String sql2 ="select * from  where mid='"+ uid + "'";*/
	    try {
			while (rs.next()) {
				String id_string = rs.getString("mid");
				String name = rs.getString("name");
				String type = rs.getString("type");
				String actor = rs.getString("actor");
				String length_string = rs.getString("length");
				String director = rs.getString("director");
				String price_string = rs.getString("price");
				String mark_string = rs.getString("mark");
				String introduce = rs.getString("introduce");
				String picture = rs.getString("picture");
				
				int id = Integer.parseInt(id_string);
				int length = Integer.parseInt(length_string);
				float price = Float.parseFloat(price_string);
				float mark = Float.parseFloat(mark_string);
				
				Movie m = new Movie();
				m.setMid(id);
				m.setName(name);			
				m.setType(type);
				m.setActor(actor);
				m.setLength(length);
				m.setDirector(director);
				m.setPrice(price);
				m.setMark(mark);
				m.setIntroduce(introduce);
				m.setPicture(picture);
				
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}
